package nevo.pkgs.expr;

import java.util.*;

import nevo.core.ObjectiveFunction;

import org.lsmp.djep.vectorJep.values.*;

public class RealObjectiveFunction extends RealFunction implements ObjectiveFunction
{
	protected Object computeFunctionOutput(Map<String, Object> output) throws Exception
	{
		Map<String, Object> funcInput = new HashMap<String, Object>();
		
		for (Map.Entry<String, Object> entry : output.entrySet()) {
			
			String varname = entry.getKey();
			Object val = entry.getValue();
			
			if ((val instanceof Double) ||
			    (val instanceof Integer) ||
			    (val instanceof Double[]) ||
			    (val instanceof Integer[]) ) {
				
				funcInput.put(varname, val);
			} else if (val instanceof MVector) {
				
				MVector v = (MVector) val;
				Double[] d = new Double[v.getNumEles()];
				for (int k = 0; k < v.getNumEles(); k++) {
					d[k] = (Double) v.getEle(k);
				}
				funcInput.put(varname, d);
			}
		}
		
		Object funcOutput = evaluate(funcInput);				
		return funcOutput;
	}
	
	public double compute(Map<String, Object> modelOutput) throws Exception
	{
		Object output = computeFunctionOutput(modelOutput);
	
		if (output instanceof Double) {
			return (Double) output;
		} else {
			throw new Exception("The objective function '" + expression + "' must return a scalar!");
		}
	}
}
